package controller;


import entity.MyException;
import entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
public class MyController {

    @RequestMapping("/hello")
    public  String first() throws MyException {
        int a=2,b=0;
        if(b==0){
            throw new MyException("分母为0了");
        }else{
            int c=a/b;
        }

        return "hello";
    }

    @RequestMapping("/hello2/{name}/{age}")
    @ResponseBody
    public  String second(@PathVariable String name, @PathVariable Integer age){
        System.out.println("我是Controller中的second,参数是name==="+name+"----age==="+age);
        return "hello";
    }



    @RequestMapping("/my")
    public  String my(HttpServletRequest req){
        req.setAttribute("name","abcd");
        return "my";
    }


    @RequestMapping("/stu")
    @ResponseBody
    public Student student(){
        Student student=new Student();
        student.setName("tom");
        student.setAge(20);
        student.setGender("男");
        return student;
    }
 // * Controller.MyController.student(int)
    @RequestMapping("/stu2")
    @ResponseBody
    public Student student(int a){
        Student student=new Student();
        student.setName("tom");
        student.setAge(20);
        student.setGender("男");
        return student;
    }


    @RequestMapping("/stuList")
    @ResponseBody
    public List<Student> studentList(@RequestParam String msg,@RequestParam String info){
        System.out.println("msg=="+msg+"---info==="+info);
        List<Student> list=new ArrayList<>();
        Student student=new Student();
        student.setName("tom");
        student.setAge(20);
        student.setGender("男");

        Student student2=new Student();
        student2.setName("tom2");
        student2.setAge(22);
        student2.setGender("女");
        list.add(student);
        list.add(student2);
        return list;
    }



    @RequestMapping("/aspect")
    @ResponseBody
    public String aspect(){
        int a=2/0;
        System.out.println("我是aspect");
        return "hello Aspect!";
    }


}
